﻿namespace ZOIL.Library.Behaviors
{
    //public static class zobjectmousehandler
    //{
    //    private static dictionary<zcomponent, zobjectmanipulationinformation> elementsbeingmanipulated = new dictionary<zcomponent, zobjectmanipulationinformation>();
    //    private static dictionary<affine2dinertiaprocessor, zobjectmanipulationinformation> elementsbeingaccelerated = new dictionary<affine2dinertiaprocessor, zobjectmanipulationinformation>();

    //    public static void onmousedown(object sender, mousebuttoneventargs e)
    //    {
    //        zcomponent comp = sender as zcomponent;

    //        if(comp != null)
    //        {
    //            zobjectmanipulationinformation info = null;
    //            if (!elementsbeingmanipulated.containskey(comp))
    //            {
    //                info = new zobjectmanipulationinformation(comp, zobjectdragdropbehavior.getisdraggable(comp), zobjectresizebehavior.getisresizable(comp), zobjectrotatebehavior.getisrotatable(comp));
    //                elementsbeingmanipulated[comp] = info;

    //                info.manipulationprocessor.affine2dmanipulationstarted += new eventhandler<affine2doperationstartedeventargs>(manipulationprocessor_affine2dmanipulationstarted);
    //                info.manipulationprocessor.affine2dmanipulationdelta += new eventhandler<affine2doperationdeltaeventargs>(manipulationprocessor_affine2dmanipulationdelta);
    //                info.manipulationprocessor.affine2dmanipulationcompleted += new eventhandler<affine2doperationcompletedeventargs>(manipulationprocessor_affine2dmanipulationcompleted);
    //                info.parentlandscapes = ztreehelper.getlistofparents<zinformationlandscape>(comp);
    //                info.left = canvas.getleft(comp);
    //                info.top = canvas.gettop(comp);
    //            }
    //            else
    //            {
    //                info = elementsbeingmanipulated[comp];
    //            }
    //            comp.capturemouse();
    //            info.manipulationprocessor.begintrack(e.device as mousedevice);
    //            info.manipulationprocessor.completemanipulation();
    //            info.manipulationprocessor.endtrack(e.device as mousedevice);
    //        }
    //        e.handled = true;
    //    }

    //    static void manipulationprocessor_affine2dmanipulationstarted(object sender, affine2doperationstartedeventargs e)
    //    {
    //        throw new notimplementedexception();
    //    }

    //    public static void onmouseup(object sender, mousebuttoneventargs e)
    //    {
    //        zcomponent comp = sender as zcomponent;
    //        if (comp != null)
    //        {
    //            comp.releasemousecapture();
    //        }
    //    }

    //    #region manipulation

    //    static void manipulationprocessor_affine2dmanipulationdelta(object sender, affine2doperationdeltaeventargs e)
    //    {
    //        affine2dmanipulationprocessor processor = sender as affine2dmanipulationprocessor;

    //        zcomponent comp = processor.container as zcomponent;
    //        if (comp != null)
    //        {
    //            if (elementsbeingmanipulated.containskey(comp))
    //            {
    //                zobjectmanipulationinformation info = elementsbeingmanipulated[comp];

    //                if (zobjectresizebehavior.getisresizable(comp) && e.scaledelta != 1)
    //                {
    //                    double oldwidth = info.componentbeingmanipulated.width;
    //                    double oldheight = info.componentbeingmanipulated.height;

    //                    info.componentbeingmanipulated.width *= e.scaledelta;
    //                    info.componentbeingmanipulated.height *= e.scaledelta;

    //                    double xdiff = info.componentbeingmanipulated.width - oldwidth;
    //                    double ydiff = info.componentbeingmanipulated.height - oldheight;

    //                    canvas.setleft(info.componentbeingmanipulated, canvas.getleft(info.componentbeingmanipulated) - (xdiff / 2.0));
    //                    canvas.settop(info.componentbeingmanipulated, canvas.gettop(info.componentbeingmanipulated) - (ydiff / 2.0));
    //                }

    //                if (zobjectdragdropbehavior.getisdraggable(comp))
    //                {
    //                    //if we are not dragging yet fire drag started event and set is draggiing to true
    //                    if (!zobjectdragdropbehavior.getisdragging(comp))
    //                    {
    //                        zobjectdragdropeventargs args = new zobjectdragdropeventargs();
    //                        args.routedevent = zobjectdragdropbehavior.dragstartedevent;
    //                        args.source = comp;
    //                        args.currentdragpoint = e.manipulationorigin;
    //                        comp.raiseevent(args);
    //                        zobjectdragdropbehavior.setisdragging(comp, true);
    //                    }
    //                    //otherwise fire only drag delta event
    //                    else
    //                    {
    //                        //raise dragdeltaevent
    //                        zobjectdragdropeventargs args = new zobjectdragdropeventargs();
    //                        args.currentdragpoint = e.manipulationorigin;
    //                        args.routedevent = zobjectdragdropbehavior.dragdeltaevent;
    //                        args.source = comp;
    //                        comp.raiseevent(args);
    //                    }

    //                    double scale = info.getscalefactor();

    //                    canvas.setleft(comp, info.left + e.cumulativetranslation.x / scale);
    //                    canvas.settop(comp, info.top + e.cumulativetranslation.y / scale);
    //                }

    //                if (zobjectrotatebehavior.getisrotatable(comp))
    //                {
    //                    comp.angle += e.rotationdelta;
    //                }
    //            }
    //        }
    //    }

    //    static void manipulationprocessor_affine2dmanipulationcompleted(object sender, affine2doperationcompletedeventargs e)
    //    {
    //        affine2dmanipulationprocessor processor = sender as affine2dmanipulationprocessor;

    //        zcomponent comp = processor.container as zcomponent;
    //        if (comp != null)
    //        {
    //            if (elementsbeingmanipulated.containskey(comp))
    //            {
    //                zobjectmanipulationinformation info = elementsbeingmanipulated[comp];

    //                elementsbeingaccelerated[info.inertiaprocessor] = info;
    //                elementsbeingmanipulated.remove(comp);

    //                info.left = canvas.getleft(comp);
    //                info.top = canvas.gettop(comp);
    //                info.inertiaprocessor.initialorigin = e.manipulationorigin;
    //                info.inertiaprocessor.initialvelocity = e.velocity;
    //                info.inertiaprocessor.initialangularvelocity = e.angularvelocity;
    //                info.inertiaprocessor.affine2dinertiadelta += new eventhandler<affine2doperationdeltaeventargs>(inertiaprocessor_affine2dinertiadelta);
    //                info.inertiaprocessor.affine2dinertiacompleted += new eventhandler<affine2doperationcompletedeventargs>(inertiaprocessor_affine2dinertiacompleted);
    //                info.inertiaprocessor.desiredangulardeceleration = 0.000001;
    //                info.inertiaprocessor.desireddeceleration = 0.001;
    //                info.inertiaprocessor.elasticmargin = new system.windows.thickness(50);
    //                info.inertiaprocessor.bounds = new system.windows.thickness(50, 50, window.getwindow(comp).actualwidth - 50, window.getwindow(comp).actualheight - 50);

    //                info.inertiaprocessor.begin();
    //            }
    //        }
    //    }

    //    #endregion

    //    #region intertia

    //    static void inertiaprocessor_affine2dinertiacompleted(object sender, affine2doperationcompletedeventargs e)
    //    {
    //        affine2dinertiaprocessor processor = sender as affine2dinertiaprocessor;

    //        if (elementsbeingaccelerated.containskey(processor))
    //        {
    //            zobjectmanipulationinformation info = elementsbeingaccelerated[processor];
    //            //raise dragfinishedevent and set isdragging to false
    //            zobjectdragdropeventargs args = new zobjectdragdropeventargs();
    //            args.routedevent = zobjectdragdropbehavior.dragfinishedevent;
    //            args.source = info.componentbeingmanipulated;
    //            args.currentdragpoint = e.manipulationorigin;
    //            info.componentbeingmanipulated.raiseevent(args);
    //            zobjectdragdropbehavior.setisdragging(info.componentbeingmanipulated, false);

    //            elementsbeingaccelerated.remove(processor);
    //        }
    //    }

    //    static void inertiaprocessor_affine2dinertiadelta(object sender, affine2doperationdeltaeventargs e)
    //    {
    //        affine2dinertiaprocessor processor = sender as affine2dinertiaprocessor;

    //        if (elementsbeingaccelerated.containskey(processor))
    //        {
    //            zobjectmanipulationinformation info = elementsbeingaccelerated[processor];

    //            zcomponent comp = info.componentbeingmanipulated;

    //            if (zobjectdragdropbehavior.getisdraggable(comp))
    //            {
    //                //raise dragdeltaevent
    //                zobjectdragdropeventargs args = new zobjectdragdropeventargs();
    //                args.currentdragpoint = e.manipulationorigin;
    //                args.routedevent = zobjectdragdropbehavior.dragdeltaevent;
    //                args.source = comp;
    //                comp.raiseevent(args);

    //                double scale = info.getscalefactor();

    //                canvas.setleft(comp, info.left + e.cumulativetranslation.x / scale);
    //                canvas.settop(comp, info.top + e.cumulativetranslation.y / scale);
    //            }

    //            if (zobjectrotatebehavior.getisrotatable(comp))
    //            {
    //                comp.angle += e.rotationdelta;
    //                console.writeline("angle velocity: " + e.angularvelocity);
    //            }
    //        }
    //    }
    //    #endregion
    //}
}